﻿//Author: Pham Thi Kim Diep
// Group : 06TH1d.12
// Created : 12/5/2010
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Windows.Forms;

namespace QUANLYNHASACH.App_code.BLL
{
    public class SACH_BLL
    {
        #region LẤY CHI TIẾT SÁCH
        public List<DAL.SACH_td> LayChiTietSach()
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "getSach";
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataReader reader = cmd.ExecuteReader();

                    List<DAL.SACH_td> sachLST = new List<DAL.SACH_td>();

                    while (reader.Read())
                    {
                        DAL.SACH_td sach = new DAL.SACH_td();

                        sach.idSach = reader[0].ToString();
                        sach.idTheLoai = reader[1].ToString();
                        sach.idNhaXuatBan = reader[2].ToString();
                        sach.TenSach = reader[3].ToString();
                        sach.TacGia = reader[4].ToString();
                        sach.DichGia = reader[5].ToString();
                        sach.SoTrang = int.Parse(reader[6].ToString());
                        sach.KichThuoc = float.Parse(reader[7].ToString());
                        sach.HinhThuc = reader[8].ToString();
                        sach.TrongLuong = float.Parse(reader[9].ToString());
                        sach.GiaBan = float.Parse(reader[10].ToString());
                        sach.SLTrongKho = int.Parse(reader[11].ToString());

                        sachLST.Add(sach);

                    }
                    conn.Close();
                    return sachLST;
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi xảy ra: " + ex.Message, "Thông báo lỗi");
            }
            return null;
        }
        #endregion

        #region THÊM SÁCH
        public void ThemSach(string idSach, string idTheLoai, string idNhaXuatBan, string TenSach, string TacGia, string DichGia, int SoTrang, float KichThuoc, string HinhThuc, float TrongLuong, float GiaBan, int SLTrongKho)
        {
            //bool result = false;
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.connect.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "addSach";
                    cmd.CommandType = CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idSach";
                    param.Value = idSach;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@idTheLoai";
                    param.Value = idTheLoai;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@idNhaXuatBan";
                    param.Value = idNhaXuatBan;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@TenSach";
                    param.Value = TenSach;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@TacGia";
                    param.Value = TacGia;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@DichGia";
                    param.Value = DichGia;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@SoTrang";
                    param.Value = SoTrang;
                    param.DbType = DbType.Int32;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@KichThuoc";
                    param.Value = KichThuoc;
                    param.DbType = DbType.Double;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@HinhThuc";
                    param.Value = HinhThuc;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@TrongLuong";
                    param.Value = TrongLuong;
                    param.DbType = DbType.Double;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@GiaBia";
                    param.Value = GiaBan;
                    param.DbType = DbType.Double;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@SLTrongKho";
                    param.Value = SLTrongKho;
                    param.DbType = DbType.Int32;
                    cmd.Parameters.Add(param);

                    cmd.ExecuteNonQuery();
              //      result = true;
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
                //result = false;
            }

            //return result;

        }
        #endregion

        #region XÓA SÁCH
        public void xoasach(string idsach)
        {
            //bool result = false;

            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delSach";
                    cmd.CommandType = CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idSach";
                    param.Value = idsach;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    cmd.ExecuteNonQuery();
                   // result = true;
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
                //result = false;
            }
            //return result;
        }
        #endregion

        #region SỬA SÁCH
        public void suasach(string idsach, string idTheLoai_new, string idNhaXuatBan_new, string TenSach_new, string TacGia_new, string DichGia_new, int SoTrang_new, float KichThuoc_new, string HinhThuc_new, float TrongLuong_new, float GiaBia_new, int SLTrongKho_new)
        {

            //bool result = false;
            try {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "editSach";
                    cmd.CommandType = CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idSach";
                    param.Value = idsach;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@idTheLoai_new";
                    param.Value = idTheLoai_new;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@idNhaXuatBan_new";
                    param.Value = idNhaXuatBan_new;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@TenSach_new";
                    param.Value = TenSach_new;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@TacGia_new";
                    param.Value = TacGia_new;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@DichGia_new";
                    param.Value = DichGia_new;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@SoTrang_new";
                    param.Value = SoTrang_new;
                    param.DbType = DbType.Int32;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@KichThuoc_new";
                    param.Value = KichThuoc_new;
                    param.DbType = DbType.Double;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@HinhThuc_new";
                    param.Value = HinhThuc_new;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@TrongLuong_new";
                    param.Value = TrongLuong_new;
                    param.DbType = DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@GiaBia_new";
                    param.Value = GiaBia_new;
                    param.DbType = DbType.Double;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@SLTrongKho_new";
                    param.Value = SLTrongKho_new;
                    param.DbType = DbType.Int32;
                    cmd.Parameters.Add(param);

                    cmd.ExecuteNonQuery();
                 //   result = true;
                    conn.Close();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
               // result = false;
            }

           // return result;
        }
        #endregion
    }
}
